/**
* Field consumer that shows the value of specified attribute to GUI
**/
jsx3.Class.defineClass(
    "ux.cdb.admin.Privilege",
    null,
    null,
    function (privilege) {
        privilege.loaded = false;
        privilege.privilData = new Object();
        privilege.callback = null;
        privilege.user = null;
        privilege.loadPrivilege = function(callback){
            privilege.callback = callback;
            privilege.log("loading privilege.");
            ux.megatron.util.Dwr.callDwr("LoginUCC", "getUserDetail", null, privilege, privilege._callback, "LoginUCC");
        };
        privilege.getUser = function(){
            return privilege.user;
        };
        privilege.getProvinceCode = function(){
            return privilege.provinceCode;
        };
        privilege.getMenuData = function() {
            return privilege.menuData;
        };
        privilege.getRoleList = function() {
            return privilege.roleList;
        };
        privilege._callback = function(dwrObject){
            try{
                privilege.log("privilege._callback.");
                if(dwrObject && dwrObject.content && dwrObject.content.data){
                    var privilegeData = dwrObject.content.data[1];
                    privilege.menuData = dwrObject.content.data[1];
                    privilege.user = dwrObject.content.data[0].user;
                    privilege.roleList = dwrObject.content.data[0].listRole;
                    privilege.provinceCode = dwrObject.content.data[2];
                    var _walkObject = function(object){
                        if(object){
                            var privilData = {};
                            if(object["children"]){
                                for(var i = 0; i < object["children"].length; i ++){
                                    privilData[object["children"][i]["id"]] = _walkObject(object["children"][i]);
                                };
                            }
                            return privilData;
                        } else {
                            return null;
                        }
                    };
                    privilege.privilData = _walkObject({"children": privilegeData});
                    privilege.loaded = true;
                    if(privilege.callback)
                        privilege.callback();
                } else {
                    privilege.log("Dwr error when loading privilege.");
                }
            } catch(ex){
                console.log(ex);
            }
        }
        privilege.getPrivilege = function(moduleCode){
            var rtnPrivil = privilege.privilData;
            var stack = [];
            if(moduleCode){
                var _walkPrivilege = function(privilObject){
                    if(privilObject[moduleCode]){
                        return privilObject[moduleCode];
                    } else {
                        for(var p in privilObject){
                            var find = _walkPrivilege(privilObject[p]);
                            if(find)
                                return find;
                        }
                    }
                    return null;
                };
                return _walkPrivilege(rtnPrivil);
            } else {
                return rtnPrivil;
            }
        };

        privilege.log = function(log){
            jsx3.log("ux.cdb.admin.Privilege: " + log);
        };
    });